#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;



int main(){
	int ans=0;
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	vector<int> f[n];
	int dp[n];
	int edge[n];
	memset(edge,0,sizeof(edge));
	memset(dp,0,sizeof(dp));
	for(int i=0;i<n-1;i++){
		int s,t;
		cin>>s>>t;
		s--;
		t--;
		edge[s]++;
		edge[t]++;
		f[s].push_back(t);
		f[t].push_back(s);
	}
	
	
	
	queue<int> q;
	for(int i=0;i<n;i++)
		if(edge[i] == 1)
			q.push(i);
	
	while(!q.empty()){
		int now;
		now = q.front();
		if(edge[now] == 0){
			ans = max(ans,dp[now]+a[now]);
			break;
		}
		q.pop();
		int index;
		for(int i=0;i<f[now].size();i++){
			if(edge[f[now][i]] > 1){
				index = i;
				break;
			}
		}
		edge[f[now][index]]--;
		if(edge[f[now][index]] == 1)
			q.push(f[now][index]);
		dp[now] += a[now];
		ans = max(ans,dp[now]);
		if(dp[now] <= 0)
			continue;
		dp[f[now][index]] += dp[now];
	}
	for(int i=0;i<n;i++)
		cout<<dp[i]<<" ";
	cout<<endl;
	cout<<ans;
	return 0;
}
